package com.klw.business.service.impl;

import java.math.BigDecimal;
import java.util.List;

import com.klw.business.domain.KlmBatchModel;
import com.klw.business.domain.KlwScores;
import com.klw.business.service.*;
import com.klw.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.klw.business.mapper.KlwMlFeaturesMapper;
import com.klw.business.domain.KlwMlFeatures;

/**
 * 机器学习特征Service业务层处理
 * 
 * @author klw
 * @date 2025-08-24
 */
@Service
public class KlwMlFeaturesServiceImpl implements IKlwMlFeaturesService 
{
    @Autowired
    private KlwMlFeaturesMapper klwMlFeaturesMapper;

    @Autowired
    private IKlwStudentsService klwStudentsService;

    @Autowired
    private IKlwScoresService klwScoresService;

    @Autowired
    private IKlwStudentAttendanceService klwStudentAttendanceService;

    @Autowired
    private IKlwStudentBehaviorService klwStudentBehaviorService;

    @Autowired
    private IKlwCoursesService klwCoursesService;

    /**
     * 查询机器学习特征
     * 
     * @param featureId 机器学习特征主键
     * @return 机器学习特征
     */
    @Override
    public KlwMlFeatures selectKlwMlFeaturesByFeatureId(Long featureId)
    {
        return klwMlFeaturesMapper.selectKlwMlFeaturesByFeatureId(featureId);
    }

    /**
     * 查询机器学习特征列表
     * 
     * @param klwMlFeatures 机器学习特征
     * @return 机器学习特征
     */
    @Override
    public List<KlwMlFeatures> selectKlwMlFeaturesList(KlwMlFeatures klwMlFeatures)
    {
        return klwMlFeaturesMapper.selectKlwMlFeaturesList(klwMlFeatures);
    }

    /**
     * 新增机器学习特征
     * 
     * @param klwMlFeatures 机器学习特征
     * @return 结果
     */
    @Override
    public int insertKlwMlFeatures(KlwMlFeatures klwMlFeatures)
    {
        klwMlFeatures.setCreateTime(DateUtils.getNowDate());
        return klwMlFeaturesMapper.insertKlwMlFeatures(klwMlFeatures);
    }

    /**
     * 修改机器学习特征
     * 
     * @param klwMlFeatures 机器学习特征
     * @return 结果
     */
    @Override
    public int updateKlwMlFeatures(KlwMlFeatures klwMlFeatures)
    {
        klwMlFeatures.setUpdateTime(DateUtils.getNowDate());
        return klwMlFeaturesMapper.updateKlwMlFeatures(klwMlFeatures);
    }

    /**
     * 批量删除机器学习特征
     * 
     * @param featureIds 需要删除的机器学习特征主键
     * @return 结果
     */
    @Override
    public int deleteKlwMlFeaturesByFeatureIds(Long[] featureIds)
    {
        return klwMlFeaturesMapper.deleteKlwMlFeaturesByFeatureIds(featureIds);
    }

    /**
     * 删除机器学习特征信息
     * 
     * @param featureId 机器学习特征主键
     * @return 结果
     */
    @Override
    public int deleteKlwMlFeaturesByFeatureId(Long featureId)
    {
        return klwMlFeaturesMapper.deleteKlwMlFeaturesByFeatureId(featureId);
    }

    /**
     * 根据参数生成特征数据
     *
     * @param klmBatchModel 学生参数
     * @return 结果
     */
    @Override
    public int generateFeatursDataSingle(KlmBatchModel klmBatchModel) {
        if(klmBatchModel.getStudentId() == null || klmBatchModel.getSemesterId() == null){
            throw new RuntimeException("参数错误");
        }
        // 判断学生是否存在
        validStudentExist(klmBatchModel.getStudentId());
        // 获取该学生平均成绩
        BigDecimal avgScore = klwScoresService.selectAvgScoreByParam(klmBatchModel);
        // 获取该学生挂科门数
        Long failCount = klwScoresService.selectFailCountByParam(klmBatchModel);
        // 获取学生考勤分数
        Long absentScore = klwStudentAttendanceService.selectAbsentScoreByParam(klmBatchModel);
        // 获取学生行为分数
        Long behaviorScore = klwStudentBehaviorService.selectBehaviorScoreByParam(klmBatchModel);
        // 获取学生学分总和
        BigDecimal creditScore = klwCoursesService.selectCreditScoreByParam(klmBatchModel);
        // 记录该学生制定学期特征值
        KlwMlFeatures klwMlFeatures = new KlwMlFeatures();
        klwMlFeatures.setStudentId(klmBatchModel.getStudentId());
        klwMlFeatures.setSemesterId(klmBatchModel.getSemesterId());
        klwMlFeatures.setAvgScore(avgScore);
        klwMlFeatures.setFailCount(failCount);
        klwMlFeatures.setAbsentScore(absentScore);
        klwMlFeatures.setBehaviorScore(behaviorScore);
        klwMlFeatures.setCreditScore(creditScore);
        KlwMlFeatures exist = klwMlFeaturesMapper.selectFeatureByParam(klmBatchModel);
        if(exist == null){
            return klwMlFeaturesMapper.insertKlwMlFeatures(klwMlFeatures);
        }else{
            klwMlFeatures.setFeatureId(exist.getFeatureId());
            return klwMlFeaturesMapper.updateKlwMlFeatures(klwMlFeatures);
        }
    }

    @Override
    public int generateFeatursDataBatch(List<KlmBatchModel> list) {
        int rows = 0;
        for (KlmBatchModel klmBatchModel : list) {
            rows += generateFeatursDataSingle(klmBatchModel);
        }
        return rows;
    }

    /**
     * 验证学生是否存在
     */
    private void validStudentExist(Long studentId){
        if(klwStudentsService.selectKlwStudentsByStudentId(studentId) == null){
            throw new RuntimeException("学生不存在");
        }
    }
}
